package br.ufmg.ubicomp.droidguide.application;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Spinner;
import br.ufmg.ubicomp.droidguide.R;
import br.ufmg.ubicomp.droidguide.common.DroidGuideConfigurationManager;
import br.ufmg.ubicomp.droidguide.common.DroidGuideConstants;
import br.ufmg.ubicomp.droidguide.context.Context;
import br.ufmg.ubicomp.droidguide.eventservice.DroidGuideEventServiceFactory;
import br.ufmg.ubicomp.droidguide.eventservice.DroidGuideServerRequestService;
import br.ufmg.ubicomp.droidguide.utils.AndroidUtils;

public class HowAmIActivity extends DroidGuideScreenActivity implements OnClickListener {

	private Spinner hunger, mood, provision, sleep, time; 
	private Button ok;
	private Context theContext;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.howamiactivity);
		
		hunger = (Spinner) findViewById(R.id.sliderHunger);
		mood = (Spinner) findViewById(R.id.sliderMood);
		provision = (Spinner) findViewById(R.id.sliderExplore);
		sleep = (Spinner) findViewById(R.id.sliderSleep);
		time = (Spinner) findViewById(R.id.sliderTime);
		
		ok = (Button) findViewById(R.id.OK);
		
		hunger.setAdapter(createAdapter(R.array.hunger));
		mood.setAdapter(createAdapter(R.array.mood));
		provision.setAdapter(createAdapter(R.array.explorationLevel));
		sleep.setAdapter(createAdapter(R.array.sleep));
		time.setAdapter(createAdapter(R.array.timeAvailable));	
		
		ok.setOnClickListener(this);

		Thread t = new Thread(){
			@Override
			public void run() {
				loadPredefinedValuesIfNecessary();
			}
		};
		
		AndroidUtils.runTask(t, 100);
	}
	
	public void loadPredefinedValuesIfNecessary()
	{
		String userid = DroidGuideConfigurationManager.getInstance().getData(
				DroidGuideConstants.DROID_GUIDE_USER_KEY);
		DroidGuideServerRequestService service = DroidGuideEventServiceFactory.getServerRequestService();
		try {
			AndroidUtils.showAlert(this, "Loading your context . . .");
			theContext = service.getUserContext(userid);
			
			if (theContext != null)
			{
				hunger.getAdapter().getItem(theContext.getHunger());
				hunger.setSelection(theContext.getHunger());
				mood.setSelection(theContext.getMood());
				provision.setSelection(theContext.getProvision());
				sleep.setSelection(theContext.getSleep());
				time.setSelection(theContext.getTime());
				AndroidUtils.showAlert(this, "Context loaded successfully.");
			}			
		} catch (Exception e) {
			e.printStackTrace();
			notifyError(e, "getContext");
		}
	}
	
	@Override
	public void onClick(View arg0) {
		
		if (theContext == null)
		{
			theContext = new Context();
		}
		theContext.setHunger((byte) hunger.getSelectedItemPosition());
		theContext.setMood((byte) mood.getSelectedItemPosition());
		theContext.setProvision((byte) provision.getSelectedItemPosition());
		theContext.setSleep((byte) sleep.getSelectedItemPosition());
		theContext.setTime((byte) time.getSelectedItemPosition());
		
		saveContext();
		
	}

	private void saveContext() {
		Thread t = new Thread(){
			@Override
			public void run() {
				String userid = DroidGuideConfigurationManager.getInstance().getData(
						DroidGuideConstants.DROID_GUIDE_USER_KEY);
				DroidGuideServerRequestService service = DroidGuideEventServiceFactory.getServerRequestService();				
				try {
					theContext = service.saveUserContext(theContext, userid);
					if (theContext.getKey() != null && !theContext.getKey().trim().equals(""))
					{
						showAlert("Context saved successfully.");
					}
					finish();
				} catch (Exception e) {
					e.printStackTrace();
					notifyError(e, "saveContext");
				}
			}
		};
		
		AndroidUtils.runTask(t, 0);
	}
	
}
